﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using TccServicos.Models;
using TccServicos.Util;

namespace TccServicos.Controllers
{
    public class UfController : ApiController
    {
        #region Atributos
        private string sThread = "";
        #endregion

        #region WebApis do TCC UF
        [HttpPost]
        public RetornoServico SaveUf(Uf objeto)
        {
            sThread = Util.Util.NextHex(4);
            string nomeFuncao = string.Format("{0} [{1}]", "SaveUf", sThread);

            try
            {
                if (!Validacao.ObjetoValido(objeto))
                {
                    Log.GravarLog(string.Format("{0}: {1}", nomeFuncao, Validacao.objetoInvalido));
                }

                string jsonEntrada = JsonConvert.SerializeObject(objeto, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore
                });

                Log.GravarLog(string.Format("{0}: {1}", nomeFuncao, jsonEntrada));

                using (adoteEntities contexto = new adoteEntities())
                {
                    contexto.CADASTRO_UF.Add(new CADASTRO_UF
                    {
                        UF = objeto.uf,
                    });
                    contexto.SaveChanges();
                    Log.GravarLog(string.Format("{0}: {1}", nomeFuncao, Validacao.salvoComSucesso));
                    return Util.Util.RetornoOk();
                }
            }
            catch (Exception ex)
            {
                Log.GravarLog(string.Format("{0} Erro: {1}", nomeFuncao, ex.ToString()));
                return Util.Util.RetornoErro(ex.ToString());
            }
        }

        [HttpGet]
        public RetornoUf GetUf([FromUri] int? idUf = null, [FromUri] string uf = null)
        {
            sThread = Util.Util.NextHex(4);
            string nomeFuncao = string.Format("{0} [{1}]", "GetUf", sThread);

            try
            {
                Log.GravarLog(string.Format("{0}: id={1}", nomeFuncao, idUf.HasValue ? idUf.Value.ToString() : "0"));

                using (adoteEntities contexto = new adoteEntities())
                {
                    var consulta = (from t in contexto.CADASTRO_UF.AsNoTracking()
                                    select new Uf
                                    {
                                        idUf = t.ID_UF,
                                        uf = t.UF,
                                    });
                    if (idUf.HasValue)
                        consulta = consulta.Where(x => x.idUf.Equals(idUf.Value));
                    if (!string.IsNullOrEmpty(uf))
                        consulta = consulta.Where(x => x.uf.Equals(uf));
                    consulta = consulta.OrderBy(x => x.uf);

                    var retorno = consulta.ToList();

                    Log.GravarLog(string.Format("{0}: {1}", nomeFuncao, Validacao.consultaComSucesso));

                    return new RetornoUf() { lista = retorno };
                }
            }
            catch (Exception ex)
            {
                Log.GravarLog(string.Format("{0} Erro: {1}", nomeFuncao, ex.ToString()));
                return default(RetornoUf);
            }
        }
        #endregion

    }
}
